import fetch from "src/utils/fetch";
import API from "src/utils/api";
import notification from "src/utils/notification";

const strategyManage = {
  state: {
    strategyList:[],
    strategyPager:{
        rowCount:0,
    },
    rulevarlist:[],
    dutyOpts:[],
    templateListAll:[],
    orgListAll:[],

  },
  mutations: {
    QUERY_STRATEGY_LIST(state, strategyList) {
      state.strategyList=strategyList.list;
      state.strategyPager.rowCount=strategyList.rowCount;
    },
    QUERY_RuleVar_LIST(state, rulevarlist) {
      state.rulevarlist=rulevarlist ? rulevarlist : [] ;
    },
    QUERY_TemplateAll_LIST(state, templateListAll) {
      state.templateListAll=templateListAll.map((item,index)=>{
        return {
          label:item.tempName,
          value:item.id,
          tempType:item.tempType,
        }
      });
    },
    QUERY_Duty_LIST(state, dutyOpts) {

      state.dutyOpts=[];

      dutyOpts.list.map((item,index)=>{

        item.rank > 0 && state.dutyOpts.push({
          label:item.dutyName,
          value:item.id,
        })

      })
    },
    QUERY_OrgAll_LIST(state, orgListAll) {
      state.orgListAll=orgListAll.map((item,index)=>{
        return {
          label:item.orgName,
          value:item.id,
        }
      })
    },
  },
  actions: {

    // 机构列表
    queryOrgListAll : ({ state, commit }, param) => {

      fetch({
        commit: commit,
        url: API.strategyConfig.strategy.orgList,
        type: "post",
        showRegionLoading: true,
        noWarningAlert:true,
        data:param ? param : {},
        success: function(res) {
          commit("QUERY_OrgAll_LIST", res);
        },
        error: function(res) {
          notification.error(res.returnMsg);
        }
      });
    },

    // 模板列表
    queryDutyListAll : ({ state, commit }, param) => {

      fetch({
        commit: commit,
        url: API.strategyConfig.strategy.dutyList,
        type: "post",
        showRegionLoading: true,
        noWarningAlert:true,
        data:{rank:1},
        success: function(res) {
          commit("QUERY_Duty_LIST", res);
        },
        error: function(res) {
          notification.error(res.returnMsg);
        }
      });
    },
    // 模板列表
    queryTemplateListAll : ({ state, commit }, param) => {

      fetch({
        commit: commit,
        url: API.strategyConfig.strategy.templateListAll,
        type: "post",
        showRegionLoading: true,
        noWarningAlert:true,
        data: param ? param : {},
        success: (res) =>{
          commit("QUERY_TemplateAll_LIST", res);
        },
        error: function(res) {
          notification.error(res.returnMsg);
        }
      });
    },
    // 策略变量列表
    queryStrategyList: ({ state, commit }, param) => {

      fetch({
        commit: commit,
        url: API.strategyConfig.strategy.queryStrategy,
        type: "post",
        showRegionLoading: true,
        data:param ? param : {},
        success: function(res) {
          commit("QUERY_STRATEGY_LIST", res);
        },
        error: function(res) {
          notification.error(res.returnMsg);
        }
      });
    },
    getRuleVarlist: ({ state, commit }, param) => {

      fetch({
        commit: commit,
        url: API.strategyConfig.strategy.rulevarlist,
        type: "post",
        noWarningAlert:true,
        showRegionLoading: true,
        ...param,
      });
    },
      // 保存策略变量
      saveStrategy: ({ state, commit }, options) => {
          let that=this;
          fetch({
            commit: commit,
            url: API.strategyConfig.strategy.saveStrategy,
            type: "post",
            ...options

          });
        },

         // 修改策略变量
      editStrategy: ({ state, commit }, param) => {

          fetch({
            commit: commit,
            url: API.strategyConfig.strategy.editStrategy,
            type: "post",
            data:param ? param : {},
            success: function(res) {
              notification.success("策略修改成功!");
            },
            error: function(res) {
              notification.error(res.returnMsg);
            }
          });
        },
        // 删除策略变量
      deleteStrategy: ({ state, commit }, param) => {

          fetch({
            commit: commit,
            url: API.strategyConfig.strategy.deleteStrategy,
            type: "post",
            data:param ? param : {},
            success: function(res) {
              notification.success("策略删除成功!");
            },
            error: function(res) {
              notification.error(res.returnMsg);
            }
          });
        }
  },
  getters: {
    strategyList:(state)=>state.strategyList,
    strategyPager:(state)=>state.strategyPager,
    rulevarlist:(state)=>state.rulevarlist,
    templateListAll:(state)=>state.templateListAll,
    dutyOpts:(state)=>state.dutyOpts,
    orgListAll:(state)=>state.orgListAll,

  }
};

export default strategyManage;
